﻿using System;
using XDAFacebook;
using XFControls.Utils;
using XFControls.XFPanels.XFPanelItems;
using XFItemControls.XFPanels.XFPanelItems;
using XFSenseUI;
using XFSenseUI.XFItems;
using XFSenseUI.XFItems.SenseControls;

namespace XFControls.XFPanels
{
    public partial class XFPanelTextInput : SenseListPanel
    {
        public bool Cancel { get; set; }

        
        public XFPanelTextInput()
        {
            InitializeComponent();
            Add(new XFItemBackHeader());

            var tb = new SenseItemTextBox();
            tb.SetHeight(150);

            AddSenseItem(tb);
            var buttons = new XFItemTwoButtons();

            SenseControlButton rb = buttons.RightButton;
            SenseControlButton lb = buttons.LeftButton;

            rb.ButtonText = Settings.LocalizationStrings.Submit;
            lb.ButtonText = Settings.LocalizationStrings.Cancel;

            lb.Clicked += (o, e) => EnableSlide();

            rb.Clicked += (o, e) =>
                              {
                                  Cancel = false;
                                  if (Submitted != null)
                                      Submitted(this, new TextEventArgs(tb.Text));

                                  if (!Cancel)
                                      EnableSlide();
                              };

            rb.Style.Padding = 25;
            lb.Style.Padding = 25;

            Add(buttons);
        }

        public void ClearHistory()
        {
            for (int i = 0; i < ItemCount; i++)
            {
                while (i < ItemCount && this[i] is XFItemSimpleText)
                    ClearItem(i);
            }
        }

        public void AddHistory(string[] lines)
        {
            foreach (string s in lines)
            {
                Add(s);
            }
        }

        public event EventHandler<TextEventArgs> Submitted;

        protected override void Dispose()
        {
            base.Dispose();
            Submitted = null;
        }
    }
}